import java.util.Arrays;
import java.util.stream.Stream;

/**
 * @author zhu
 * @date 2024-04-16
 */
public class MainTest {
    public static void main(String[] args) {
        Student[] students = {
                new Student("小明",18),
                new Student("张三",19),
                new Student("李四",20),
                new Student("李白",18),
                new Student("李唐",20),
        };

        Stream<Student> stream = Arrays.stream(students);

        // 过滤姓李的学生
        // Stream<Student> nameStream = stream.filter(t -> t.getName().startsWith("李"));

        // 过滤年龄大于18岁的学生
        // Stream<Student> ageStream = stream.filter((t) -> t.getAge() > 18);

        // 根据年龄大到小排序
        Stream<Student> sortStream = stream.sorted((t1, t2) -> t2.getAge() - t1.getAge());

        // 把学生对象转换（映射）为姓名字符串，循环遍历输出结果（只需要前面两个）
        Stream<String> mapStream = sortStream.map(Student::getName).limit(2);

        mapStream.forEach(System.out::println);
    }
}
